

/**
 * @author LKQ
 * @date 2022/3/4 9:39
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        TreeNode t5 = new TreeNode(5), t1 = new TreeNode(1), t0 = new TreeNode(0),
                t9 = new TreeNode(9, t5, t1), t4 = new TreeNode(4, t9, t0);
        solution.sumNumbers(t4);
    }
    public int sumNumbers(TreeNode root) {
        return dfs(root, 0);
    }
    public int dfs(TreeNode root, int prevSum) {
        if (root == null) {
            return 0;
        }
        int sum = prevSum * 10 + root.val;
        if (root.left == null && root.right == null) {
            return sum;
        } else {
            return dfs(root.left, sum) + dfs(root.right, sum);
        }
    }
}
